﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S.Api.Core
{
    /// <summary>查询条件 And Or 查询 不支持嵌套</summary>
    public class Filter : IEqualityComparer<Filter>
    {
        /// <summary>And 连接 默认</summary>
        public bool isAnd { get; set; } = true;

        /// <summary>字段名称</summary>
        public string field { get; set; }

        /// <summary> 操作符 like 、 = 、！= 、> 、< 、>= 、<= 、in 、 between </summary>
        public string op { get; set; } = "=";

        /// <summary>值</summary>
        public object value { get; set; }

        public static List<Filter> Filters(params Filter[] filter)
        {
            var filers = new List<Filter>(filter);
            return filers.Distinct(new Filter()).ToList();
        }

        public bool Equals(Filter me, Filter other)
        {
            var result = me.field == other.field
                && me.value.ToString() == other.value.ToString()
                && me.op == other.op
                && me.isAnd == other.isAnd;
            return result;
        }

        public int GetHashCode(Filter me)
        {
            return me.ToString().GetHashCode();
        }
    }

    /// <summary>排序</summary>
    public class Sort
    {
        /// <summary>字段</summary>
        public string field { get; set; }

        /// <summary>排序类型</summary>
        public string sort { get; set; } = "ASC";
    }
}
